home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / edit / jed207.lha / src / hash.lha / hash.c < prev    next >
C/C++ Source or Header  |  1993-01-06  |  3KB  |  148 lines

  1.  
  2. /*
  3.  * HASH.C
  4.  * (c) 1992-3 J.Harper
  5.  *
  6.  * open hashing module
  7.  */
  8.  
  9. #include "hash.h"
  10. #include <string.h>
  11.  
  12. /*
  13.  * _very_ simple hash function
  14.  */
  15. static int
  16. hash(char *name, int tabSize)
  17. {
  18.     int total = 0;
  19.     char c;
  20.     while(c = *name++)
  21.     total += c;
  22.     return(total % tabSize);
  23. }
  24.  
  25. /****** inserthash **********************************************************
  26. *
  27. *   NAME
  28. *    inserthash -- link a hash node
  29. *
  30. *   SYNOPSIS
  31. *    inserthash(hashTab, hashNode, tabSize)
  32. *
  33. *    void inserthash(HASHNODE **, HASHNODE *, int);
  34. *
  35. *   FUNCTION
  36. *    Links an initialized HASHNODE into a hash table, this node will
  37. *    be inserted before any other nodes with the same name. (ie, calls
  38. *    to inserthash() and removehash() nest).
  39. *
  40. *   INPUTS
  41. *    hashTab        -- pointer to an array containing tabSize number of
  42. *               pointers to HASHNODE's.
  43. *    hashNode    -- initialized HASHNODE which is to be linked into the
  44. *               hashTab
  45. *    tabSize        -- number of pointers in hashTab
  46. *
  47. *   RESULT
  48. *    none.
  49. *
  50. *   SEE ALSO
  51. *    hash.h, removehash(), findhash()
  52. *
  53. *****************************************************************************
  54. *
  55. */
  56. void
  57. inserthash(HASHNODE **hashTab, HASHNODE *node, int tabSize)
  58. {
  59.     HASHNODE *lastchain;
  60.     int index = hash(node->h_Name, tabSize);
  61.     lastchain = hashTab[index];
  62.     hashTab[index] = node;
  63.     node->h_Next = lastchain;
  64.     node->h_Prev = NULL;
  65.     if(lastchain)
  66.     lastchain->h_Prev = node;
  67. }
  68.  
  69. /****** removehash **********************************************************
  70. *
  71. *   NAME
  72. *    removehash -- unlink a hash node
  73. *
  74. *   SYNOPSIS
  75. *    removehash(hashTab, hashNode, tabSize)
  76. *
  77. *    void removehash(HASHNODE **, HASHNODE *, int);
  78. *
  79. *   FUNCTION
  80. *    Unlinks hashNode from hashTable.
  81. *
  82. *   INPUTS
  83. *    hashTab        -- pointer to the hash table
  84. *    hashNode    -- pointer to the hash node
  85. *    tabSize        -- number of pointers in hashTab
  86. *
  87. *   RESULT
  88. *    none.
  89. *
  90. *   SEE ALSO
  91. *    hash.h, inserthash(), findhash()
  92. *
  93. *****************************************************************************
  94. *
  95. */
  96. void
  97. removehash(HASHNODE **hashTab, HASHNODE *node, int tabSize)
  98. {
  99.     HASHNODE *next = node->h_Next;
  100.     HASHNODE *prev = node->h_Prev;
  101.     if(!prev)
  102.     hashTab[hash(node->h_Name, tabSize)] = next;
  103.     else
  104.     prev->h_Next = next;
  105.     if(next)
  106.     next->h_Prev = prev;
  107. }
  108.  
  109. /****** findhash ************************************************************
  110. *
  111. *   NAME
  112. *    findhash -- find a hash entry
  113. *
  114. *   SYNOPSIS
  115. *    hashNode = findhash(hashTab, name)
  116. *
  117. *    HASHNODE *findhash(HASHNODE **, char *);
  118. *
  119. *   FUNCTION
  120. *    Scans the specified hash table for the specified entry.
  121. *
  122. *   INPUTS
  123. *    hashTab        -- pointer to the hash table
  124. *    name        -- name of node to find
  125. *    tabSize        -- number of pointers in hashTab
  126. *
  127. *   RESULT
  128. *    hashNode    -- pointer to found node or NULL if no matching node
  129. *               could be found
  130. *
  131. *   SEE ALSO
  132. *    hash.h, inserthash(), removehash()
  133. *
  134. *****************************************************************************
  135. *
  136. */
  137. HASHNODE *
  138. findhash(HASHNODE **hashTab, char *name, int tabSize)
  139. {
  140.     HASHNODE *node;
  141.     for(node = hashTab[hash(name, tabSize)]; node; node = node->h_Next)
  142.     {
  143.     if(!(stricmp(name, node->h_Name)))
  144.         break;
  145.     }
  146.     return(node);
  147. }
  148.